package dynamicGui;

import dynamicGui.LobbyMenuHostE.TimerObjectSet;
import dynamicGui.LobbyMenuHostE.TimerObjectSet.RemindTask;
import gamePlay.Game;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import netcode.client.Client;
import netcode.helper.NetcodeHelper;

public class LobbyMenuGuestE extends JPanel{
	Game game_ = MenuCyclingE.game_;//used to create the game
	int gameID_ = MenuCyclingE.game_.getGameID(); //gameID for the game that the guest joined to send leave requests
	Client client_ = MenuCyclingE.client_;
	
	/*
	 * Begin Constructors---------------------------------------------------
	 */
	//default constructor
	public LobbyMenuGuestE(int screenSize) {
        super();
        this.setLayout(new GridLayout(0,1, 0, 10));
	}
	
	//Constructor that also takes in the game object
	public LobbyMenuGuestE() {
        super();
        System.out.println("DEBUG: Creating LobbyMenuGuest");

        
        this.setLayout(new GridLayout(0,1, 0, 10));
        
        createPlayerNameTextFields();//create a list of all players in the current game that user joined
        addReadyButton();
//        addunReadyButton();
        addLeaveButton();
        addFAQButton();
        
        new TimerObjectSet(3, this);//set up the timer to refresh the screen in 2 seconds
	}
    
    /*
	 * End Constructors------------------------------------------------------------------------------------------------
	 */
	
    //encapsulating the refresh method
    private void refresh(){
    	this.removeAll();
    	this.setLayout(new GridLayout(0,2, 0, 10));
    	createPlayerNameTextFields();//create a list of all players in the current game that user joined
        addReadyButton();
//        addunReadyButton();
        addLeaveButton();
        addFAQButton();
        this.validate();// re-layout everything in this panel
    }
	
	
	
	
    /*
     * Begin button Creation Methods---------------------------------------------------------------------------------
     */
	
	//Add ReadyButton
	private void addReadyButton(){
		JButton refreshButton = new JButton("Ready");
    	refreshButton.setPreferredSize(new Dimension(200, 30));
    	this.add(refreshButton);
    	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	readyButtonActionPerformed(evt);
            }
        });
	}
//	//Add un-ReadyButton
//	private void addunReadyButton(){
//		JButton refreshButton = new JButton("un-Ready");
//    	refreshButton.setPreferredSize(new Dimension(200, 30));
//    	this.add(refreshButton);
//    	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
//            public void actionPerformed(java.awt.event.ActionEvent evt) {
//            	unreadyButtonActionPerformed(evt);
//            }
//        });
//	}
	
	
	//Add LeaveButton
	private void addLeaveButton(){
		JButton refreshButton = new JButton("LeaveCurrentGame");
    	refreshButton.setPreferredSize(new Dimension(200, 30));
    	this.add(refreshButton);
    	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	leaveButtonActionPerformed(evt);
            }
        });
	}
	
	//Add FAQButton
	private void addFAQButton(){
		JButton refreshButton = new JButton("FAQ");
    	refreshButton.setPreferredSize(new Dimension(200, 30));
    	this.add(refreshButton);
    	refreshButton.addActionListener(new java.awt.event.ActionListener() {//sets up an actionlistener
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	FAQButtonActionPerformed(evt);
            }
        });
	}
	
    /*
     * End button Creation Methods------------------------------------------------------------------------------------------
     */

	
	
	
	
	
	
	
	
	
	
    /*
     * Begin textFields Creation Methods------------------------------------------------------------------------------------------
     */
	//CreateTextfields of the players in the current game
	private void createPlayerNameTextFields(){
		for (int i = 0; i < game_.getPlayers().size(); i++) {
	    	JLabel playerNameTextField = new JLabel("PlayerName: " +game_.getPlayers().get(i).getPlayer_name());
	    	playerNameTextField.setPreferredSize(new Dimension(200, 30));
	    	this.add(playerNameTextField);   	
		}
	}
    /*
     * End textFields Creation Methods------------------------------------------------------------------------------------------
     */
	
	
	
	
	
	
    /*
     *ActionListeners begin--------------------------------------------------------------------------------------------------
     */
	//Pressing leavegame Button
	private void leaveButtonActionPerformed(java.awt.event.ActionEvent evt) {
		client_.sendLeaveGameRequest(gameID_);
		//goes back to the lobby selection menu
		MenuCyclingE.displayLobbySelectionMenu();
	}
	
	//Pressing ready Button
	private void readyButtonActionPerformed(java.awt.event.ActionEvent evt) {
		//TODO
		//set status to ready
		System.out.println("DEBUG: settting to ready, waiting until game starts");
		MenuCyclingE.displayQuestionMenu();
	}
	
//	//Pressing un-ready Button
//	private void unreadyButtonActionPerformed(java.awt.event.ActionEvent evt) {
//		//TODO
//		//set status to un-ready
//		System.out.println("DEBUG: settting to un-ready");
//	}
	
	//Pressing FAQ Button
	private void FAQButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_QuickGameButtonActionPerformed
		try {
	        //Set your page url in this string. For eg, I m using URL for Google Search engine
	        String url = "http://homepages.uwp.edu/dalby001/faq.html";
	        java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
	      }
	      catch (java.io.IOException e) {
	          System.out.println(e.getMessage());
	      }
	}

    
    /*
     *ActionListeners end--------------------------------------------------------------------------------------------------
     */
	
		
	
	
	/**
	 * Waits for some time so that a message has time to be received.
	 */
	public void sleepyTime() {
		try {
			Thread.sleep(NetcodeHelper.MILLISECONDS_BETWEEN_SENDING_MESSAGE);
		} catch (Exception e) {
			System.err.println(e);
		}
	}
	
	
	
    //Inner Class to create a timer to refresh
	public class TimerObjectSet {
	  Toolkit toolkit;
	
	  Timer timer;
	
	  public TimerObjectSet(int seconds, LobbyMenuGuestE LobbyMenuGuestEIn) {
	    toolkit = Toolkit.getDefaultToolkit();
	    timer = new Timer();
	    timer.schedule(new RemindTask(LobbyMenuGuestEIn), seconds * 1000);
	    
	  }
	
	  //after time is up, do this TODO
	  class RemindTask extends TimerTask {
		LobbyMenuGuestE lobbyMenuGuestE_;
	    public RemindTask(LobbyMenuGuestE lobbyMenuHostEIn) {
	    	lobbyMenuGuestE_ = lobbyMenuHostEIn;
			// TODO Auto-generated constructor stub
		}

		public void run() {
	    	//Refresh the screen to see if we have new players that have joined the lobby
			lobbyMenuGuestE_.refresh();
//			System.out.println("DEBUG: Refreshing: timer event");
	    }
	  }
	}

}
